package cn.miwu52.homeworkerp.Controller;


import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.media.Image;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.miwu52.homeworkerp.R;
import cn.miwu52.homeworkerp.bean.Entrepot;
import cn.miwu52.homeworkerp.dao.EntrepotDao;


//仓库管理
public class EntrepotView extends AppCompatActivity {
    private EntrepotDao entrepotDao;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        entrepotDao = new EntrepotDao(this);
        setContentView(R.layout.activity_entrepot);
        Button insert_entrepot = findViewById(R.id.insert_entrepot);
        TextView entrepot_name = findViewById(R.id.entrepot_name);
        listView = findViewById(R.id.entrepotList);


        updateEntrepotList();
        //新增按钮监听事件
        insert_entrepot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (entrepot_name.getText().toString().equals("")) {
                    Toast.makeText(getApplicationContext(), "请输入仓库名！", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    boolean entrepotName = entrepotDao.isEntrepotName(entrepot_name.getText().toString());
                    if (entrepotName) {
                        boolean b = entrepotDao.insertEntrepot(entrepot_name.getText().toString());
                        if (b) {
                            Toast.makeText(getApplicationContext(), "新增成功", Toast.LENGTH_SHORT).show();
                            //新增成功后，刷新仓库列表
                            updateEntrepotList();
                        } else {
                            Toast.makeText(getApplicationContext(), "新增失败", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(getApplicationContext(), "当前仓库名已存在，无法创建", Toast.LENGTH_SHORT).show();
                    }
                    return;
                }
            }
        });
        //listView事件监听
        listView.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ImageView entrepot_change = view.findViewById(R.id.entrepot_change);
                ImageView entrepot_delete = view.findViewById(R.id.entrepot_delete);
                TextView entrepot_id = view.findViewById(R.id.entrepot_id);
                TextView entrepot_name = view.findViewById(R.id.entrepot_name);
                //修改按钮监听
                entrepot_change.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        View inflate = getLayoutInflater().inflate(R.layout.alertdialog_edit, null);
                        EditText newEntrepotName = inflate.findViewById(R.id.newEntrepotName);


                        AlertDialog alertDialog = new AlertDialog.Builder(EntrepotView.this)
                                .setIcon(R.drawable.erp)
                                .setTitle("请输入新的仓库名")
                                .setView(inflate)
                                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {

                                    }
                                })
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        boolean flag = entrepotDao.updateEntrepotById(newEntrepotName.getText().toString(), Integer.parseInt(entrepot_id.getText().toString()));
                                        if (flag) {
                                            updateEntrepotList();
                                            Toast.makeText(getApplicationContext(), "修改成功", Toast.LENGTH_SHORT).show();
                                        } else {
                                            Toast.makeText(getApplicationContext(), "修改失败", Toast.LENGTH_SHORT).show();
                                        }
                                        return;
                                    }
                                }).create();
                        alertDialog.show();
                    }
                });
                //删除按钮监听
                entrepot_delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        AlertDialog alertDialog = new AlertDialog.Builder(EntrepotView.this)
                                .setIcon(R.drawable.erp)
                                .setTitle("警告")
                                .setMessage("确定要删除【" + entrepot_name.getText().toString() + "】吗？")
                                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        System.out.println("取消删除");
                                    }
                                })
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        boolean flag = entrepotDao.deleteEntrepotById(Integer.parseInt(entrepot_id.getText().toString()));
                                        if (flag) {
                                            updateEntrepotList();
                                            Toast.makeText(getApplicationContext(), "删除成功", Toast.LENGTH_SHORT).show();
                                        } else {
                                            Toast.makeText(getApplicationContext(), "删除失败", Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                })
                                .create();
                        alertDialog.show();
                    }
                });
            }
        });
    }

    /**
     * 更新仓库列表
     */
    public void updateEntrepotList() {
        List<Entrepot> entrepotList = entrepotDao.findEntrepotList();
        //处理包装查询结果
        ArrayList<Map<String, Object>> maps = new ArrayList<>();
        for (Entrepot entrepot : entrepotList) {
            HashMap<String, Object> stringObjectHashMap = new HashMap<>();
            stringObjectHashMap.put("entrepot_name", entrepot.getEntrepotName());
            stringObjectHashMap.put("entrepot_id", entrepot.getId());
            maps.add(stringObjectHashMap);
        }
        String[] from = {"entrepot_name", "entrepot_id"};
        int[] to = {R.id.entrepot_name, R.id.entrepot_id};
        SimpleAdapter simpleAdapter = new SimpleAdapter(EntrepotView.this, maps, R.layout.enterpot_list, from, to);
        listView.setAdapter(simpleAdapter);


    }
}